# -*- coding: UTF-8 -*-

from datetime import datetime
import sys
from sqlalchemy import and_,  func, or_
from typing import Dict, List, Any, Generator

from sqlalchemy import Boolean, Column, DateTime, Integer, String, Index, Table, ForeignKey, text, JSON, TEXT, distinct
from sqlalchemy.orm import Session, relationship
from common.database import Base
from common.database import SessionMixin
from common.utils import is_empty
from core.logger import logger

__all__ = ['FSRContainerHPA']

class FSRContainerHPA(Base, SessionMixin):
    '''
    生产业务服务弹性伸缩
    '''
    __tablename__ = "fsr_container_hpa"
    name = Column(String(256), nullable=False, comment='服务名称')
    # deployment or statefulset
    classes = Column(String(128), nullable=False, index=True, comment='部署归类')
    # firing / resolved
    status = Column(String(16), default="resolved", index=True, comment='告警状态')
    # true/false
    state = Column(Boolean, default=text('true'), nullable=False, comment='使能状态')
    # true/false
    handle = Column(Boolean, default=text('False'), comment='是否操作扩容')
    number = Column(Integer, nullable=True, comment='服务当前节点数')
    max = Column(Integer, nullable=False, comment='服务最大节点数')
    min = Column(Integer, nullable=False, comment='服务最小节点数')
    update_at = Column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    comment = Column(String(256), nullable=True, comment='服务说明')


    __table_args__ = (
        Index('idx_fsr_container_hpa_name', 'name', unique=True),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic'
        }
    )

    def __repr__(self):
        return f"<fsr_container_hpa='{self.name}>"

    def object_to_dict(self, db: Session):
        result = {
            key: getattr(self, key)
            for key in self.__mapper__.c.keys()
        }

        return result




